package POI;

import java.io.*;
import java.util.Iterator;
import java.util.LinkedList;


public final class GeoInfoList extends LinkedList<GeoInfo>
{
	private static final long serialVersionUID = 5992093508109370783L;
	public GeoInfoList()
	{
		super();
	}

	public boolean loadFromFile(String fileName)
	{
		if(super.size()!=0) super.clear();
        BufferedReader in = null;
        String fileContent = new String();
        try {
            in = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
            while (in.ready()) {
            	fileContent = in.readLine();
        		String fileData[] = fileContent.split(",");
        		if(fileData.length!=5)
        			throw new IndexOutOfBoundsException("Some lines in CSV file containe more or less than 5 fields!");
				GeoInfo info;
				info=new GeoInfo(Integer.parseInt(fileData[0]),fileData[1],fileData[2],Double.parseDouble(fileData[3]+"f"),Double.parseDouble(fileData[4]+"f"));
				super.add(info);
           }
        } catch (IOException e) {
        	return false;
        }
        finally
        {
        	try {
				in.close();
			} catch (IOException e) {
				return false;
			}        	
        }
        return true;
	}
	
	public boolean saveToFile(String fileName)
	{
        BufferedWriter out = null;
            try {
				out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName)));
				Iterator<GeoInfo> it = iterator();
				while(it.hasNext())
				{
					GeoInfo val = it.next();
					out.write(val.toString());
					if(it.hasNext())out.newLine();
				}
			} catch (IOException e) {
				return false;
			}
			finally {
				try {
					out.close();
				} catch (IOException e) {
					return false;
				}
			}
			return true;
	}
	
}